
// <copyright file="TypeMappingRepository.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The type mapping repository.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Reflection.CodeSerialization
{
    using System;
    using System.Collections.Generic;
    using System.Xml.Serialization;

    /// <summary>
    /// The type mapping repository.
    /// </summary>
    [Serializable]
    public class TypeMappingRepository : AbstractInitializable
    {
        #region Fields

        /// <summary>
        /// The client to server mappings.
        /// </summary>
        private Dictionary<string, string> _clientToServerMappings;

        /// <summary>
        /// The default server method name.
        /// </summary>
        private string _defaultServerMethodName;

        /// <summary>
        /// The ignore assembly version.
        /// </summary>
        private bool _ignoreAssemblyVersion = true;

        /// <summary>
        /// The server method names.
        /// </summary>
        private Dictionary<string, string> _serverMethodNames;

        /// <summary>
        /// The server to client mappings.
        /// </summary>
        private Dictionary<string, string> _serverToClientMappings;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="TypeMappingRepository"/> class.
        /// </summary>
        public TypeMappingRepository()
        {
            this.TypeMappings = new List<TypeMapping>();
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets or sets the default server method name.
        /// </summary>
        /// <exception cref="ObjectAlreadyInitializedException">
        /// </exception>
        [XmlAttribute]
        public string DefaultServerMethodName
        {
            get
            {
                return this._defaultServerMethodName;
            }

            set
            {
                if (this.IsInitialized && this._defaultServerMethodName != value)
                {
                    throw new ObjectAlreadyInitializedException("DefaultServerMethodName cannot be changed after the object is initialized.");
                }

                this._defaultServerMethodName = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether ignore assembly version.
        /// </summary>
        /// <exception cref="ObjectAlreadyInitializedException">
        /// </exception>
        [XmlAttribute]
        public bool IgnoreAssemblyVersion
        {
            get
            {
                return this._ignoreAssemblyVersion;
            }

            set
            {
                if (this.IsInitialized && this._ignoreAssemblyVersion != value)
                {
                    throw new ObjectAlreadyInitializedException("IgnoreAssemblyVersion cannot be changed after the object is initialized.");
                }

                this._ignoreAssemblyVersion = value;
            }
        }

        /// <summary>
        /// Gets or sets the type mappings.
        /// </summary>
        public List<TypeMapping> TypeMappings { get; set; }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// The apply client to server mappings.
        /// </summary>
        /// <param name="info">
        /// The info.
        /// </param>
        public void ApplyClientToServerMappings(MethodInformation info)
        {
            this.applyMappings(info, this._clientToServerMappings);
        }

        /// <summary>
        /// The apply server to client mappings.
        /// </summary>
        /// <param name="info">
        /// The info.
        /// </param>
        public void ApplyServerToClientMappings(MethodInformation info)
        {
            this.applyMappings(info, this._serverToClientMappings);
        }

        /// <summary>
        /// The get client type by server type.
        /// </summary>
        /// <param name="serverType">
        /// The server type.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetClientTypeByServerType(string serverType)
        {
            return this.getFromDictionary(this._serverToClientMappings, serverType);
        }

        /// <summary>
        /// The get server method name.
        /// </summary>
        /// <param name="serverType">
        /// The server type.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetServerMethodName(string serverType)
        {
            return this.getFromDictionary(this._serverMethodNames, serverType);
        }

        /// <summary>
        /// The get server type by client type.
        /// </summary>
        /// <param name="clientType">
        /// The client type.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetServerTypeByClientType(string clientType)
        {
            return this.getFromDictionary(this._clientToServerMappings, clientType);
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// The initialize protected.
        /// </summary>
        protected override void InitializeProtected()
        {
            if (this.IgnoreAssemblyVersion)
            {
                this._serverToClientMappings = new Dictionary<string, string>(IgnoreAssemblyVersionEqualityComparer.Instance);
                this._clientToServerMappings = new Dictionary<string, string>(IgnoreAssemblyVersionEqualityComparer.Instance);
                this._serverMethodNames = new Dictionary<string, string>(IgnoreAssemblyVersionEqualityComparer.Instance);
            }
            else
            {
                this._serverToClientMappings = new Dictionary<string, string>();
                this._clientToServerMappings = new Dictionary<string, string>();
                this._serverMethodNames = new Dictionary<string, string>();
            }

            foreach (TypeMapping mapping in this.TypeMappings)
            {
                this._serverToClientMappings.Add(mapping.ServerType, mapping.ClientType);
                this._clientToServerMappings.Add(mapping.ClientType, mapping.ServerType);
                if (!string.IsNullOrWhiteSpace(mapping.ServerMethodName))
                {
                    this._serverMethodNames.Add(mapping.ServerType, mapping.ServerMethodName);
                }
            }
        }

        /// <summary>
        /// The apply mappings.
        /// </summary>
        /// <param name="info">
        /// The info.
        /// </param>
        /// <param name="dictionary">
        /// The dictionary.
        /// </param>
        private void applyMappings(MethodInformation info, IDictionary<string, string> dictionary)
        {
            this.Initialize();
            info.ApplyTypeMappings(dictionary);
        }

        /// <summary>
        /// The get from dictionary.
        /// </summary>
        /// <param name="dictionary">
        /// The dictionary.
        /// </param>
        /// <param name="inputType">
        /// The input type.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string getFromDictionary(IDictionary<string, string> dictionary, string inputType)
        {
            this.Initialize();
            string resultType = null;
            dictionary.TryGetValue(inputType, out resultType);
            return resultType;
        }

        #endregion Methods
    }
}
